<?php

namespace App\Services\Assess;

use App\Models\Assess\BuyerModel;
use App\Models\Assess\OperateModel;
use App\Models\Assess\OperateSkuModel;
use App\Models\Assess\OrderAndCustomersOrderModel;
use App\Models\UserModel;
use App\Services\BaseService;
use App\Services\Common\CommonService;
use App\Services\Common\RedisService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class OperateService extends BaseService
{
    //是否开启评价环节(1:是,2:否)
    public $evaluationProcess = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //是否开启注册环节(1:是,2:否)
    public $registerProcess = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //是否开启礼品环节(1:是,2:否)
    public $giftSection = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //是否开启星级评价环节(1:是,2:否)
    public $starRatingEvaluation = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //是否跳转amazon留评环节(1:是,2:否)
    public $jumpAmazonComment = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //开启跳转亚马逊留评的星星等级(1星,2星,3星)
    public $jumpAmazonStar = [
//        ['label' => '1星', 'val' => 1],
//        ['label' => '2星', 'val' => 2],
//        ['label' => '3星', 'val' => 3],
        ['label' => '4星', 'val' => 4],
        ['label' => '5星', 'val' => 5],
    ];

    public $selectMode = [
        ['label' => '常规模式', 'val' => 1],
        ['label' => '绝对安全模式', 'val' => 2]
    ];

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new OperateModel();
    }

    /**
     * @Notes:获取用户与账号管理信息
     * @Interface getList
     * @return array
     * @author: wuyu
     * @Time: 2023/6/2   19:10
     */
    public function getList()
    {
        $param = request()->all();
        // 查询条件
        $map[] = ['m.mark','=',1];
        $user = getter($param, 'user', 0);
        $account = getter($param, 'account', '');
        $sku = getter($param, "sku");
        $sort = getter($param, 'sort', 'id');
        $order = getter($param, 'order', 'desc');
        $page_size = getter($param, 'page', 1);
        $limit = getter($param, 'limit', 10);

        $skuStr = str_replace(['；', ',', '，',"\r\n", "\r", "\n"], ';', $sku);
        if (!empty($skuStr)) $skuArr = array_unique(explode(';', $skuStr));

        if (!empty($user)) $map[] = ['m.create_user', '=', $user];
        if (!empty($account)) $map[] = ['m.account_name', '=', $account];


//        $createUserAuth = CommonService::userAuth();
        $skuAuth = CommonService::skuAuth();

        $offset = ($page_size - 1) * $limit;
        $mainTable = $this->model->getTable();
        $OperateSkuModel = new OperateSkuModel();
        $sonTable = $OperateSkuModel->getTable();
        $model = $this->model::from("$mainTable as m")
            ->select('m.*')
            ->leftJoin("$sonTable as s", 's.operate_id', '=', 'm.id')
            ->distinct(true)
            ->where($map);
        $model2 = $this->model::from("$mainTable as m")
            ->select('m.id')
            ->leftJoin("$sonTable as s", 'm.id', '=', 's.operate_id')
            ->distinct('m.id')
            ->where($map);

        if (!empty($skuArr)){
            $model->whereIn('s.sku', $skuArr);
            $model2->whereIn('s.sku', $skuArr);
        }
        if ( !empty($skuAuth) && empty($skuArr)){
            $model->whereIn('s.sku', $skuAuth);
            $model2->whereIn('s.sku', $skuAuth);
        }

        $list = $model
            ->orderBy($sort, $order)
            ->offset($offset)
            ->limit(PERPAGE)
            ->get()->toArray();

        $count = $model2->count();

        $skuArr = [];
        $buyerListCol = [];
        if (!empty($list)){
            $OperateSkuModel = new OperateSkuModel();
            $BuyerModel = new BuyerModel();
            $ids = array_column($list,'id');
            $createUser = array_column($list,'create_user');
            $skuList = $OperateSkuModel->whereIn('operate_id',$ids)->get()->toArray();
            foreach ($skuList as $skuV){
                $skuArr[$skuV['operate_id']][] = $skuV['sku'];
            }
            //获取运营端配置信息

            $buyerList = $BuyerModel
                ->where('type','=',1)
                ->whereIn('create_user',$createUser)
                ->get(['gift_select_page_script','comment_page_script','amazon_comment_page_script','gift_product_page_script','gift_end_page_script','create_user','brand_code'])
                ->toArray();
//            $buyerListCol = array_column($buyerList,null,'create_user');

        }

        $UserModel = new UserModel();
        foreach ($list as $key=>$val){
            $userData = $UserModel->getOne([
                ['id','=',$val['create_user']]
            ],'id,realname');
            $val['create_user_name'] = $userData['realname'];
            $val['evaluation_process_name'] = array_column($this->evaluationProcess,'label','val')[$val['evaluation_process']] ?? '';
            $val['gift_section_name'] = array_column($this->giftSection,'label','val')[$val['gift_section']] ?? '';
            $val['register_process_name'] = array_column($this->registerProcess,'label','val')[$val['register_process']] ?? '';
            $val['star_rating_evaluation_name'] = array_column($this->starRatingEvaluation,'label','val')[$val['star_rating_evaluation']] ?? '';
            $val['jump_amazon_comment_name'] = array_column($this->jumpAmazonComment,'label','val')[$val['jump_amazon_comment']] ?? '';
            $val['jump_amazon_star_name'] = array_column($this->jumpAmazonStar,'label','val')[$val['jump_amazon_star']] ?? '';
            $val['register_page_script_operate'] = json_decode($val['register_page_script_operate'],true);
            $val['process_end_page_script_operate'] = json_decode($val['process_end_page_script_operate'],true);
            $badReviewsScript = json_decode($val['bad_reviews_script'],true);

            $val['bad_reviews_script'] = !empty($badReviewsScript['bad_reviews_script']) ? json_decode($badReviewsScript['bad_reviews_script'],true) : json_decode('{"script_1": ""}', true);
            $val['bad_reviews_script_ger'] = !empty($badReviewsScript['bad_reviews_script_ger']) ? json_decode($badReviewsScript['bad_reviews_script_ger'],true) : json_decode('{"script_1": ""}', true);
            $val['bad_reviews_script_it'] = !empty($badReviewsScript['bad_reviews_script_it']) ? json_decode($badReviewsScript['bad_reviews_script_it'],true) : json_decode('{"script_1": ""}', true);
            $val['bad_reviews_script_sp'] = !empty($badReviewsScript['bad_reviews_script_sp']) ? json_decode($badReviewsScript['bad_reviews_script_sp'],true) : json_decode('{"script_1": ""}', true);
            $val['bad_reviews_script_jp'] = !empty($badReviewsScript['bad_reviews_script_jp']) ? json_decode($badReviewsScript['bad_reviews_script_jp'],true) : json_decode('{"script_1": ""}', true);

            $val['select_mode_html'] = array_column($this->selectMode,'label','val')[$val['select_mode']] ?? '';
            if (!empty($skuArr[$val['id']])){
                $val['sku_list'] = implode(';',$skuArr[$val['id']]);
            }
            $buyerListCol = [];
            foreach ($buyerList as $vv){
                if ($vv['create_user'] == $val['create_user'] && $vv['brand_code'] == $val['brand']){
                    $buyerListCol = $vv;
                    break;
                }
            }

            $giftSelectPageScript = !empty($buyerListCol['gift_select_page_script']) ?
                json_decode($buyerListCol['gift_select_page_script'], true) :
                json_decode('{"gift_select_page_script": ""}', true);

            $val['gift_select_page_script'] = !empty($giftSelectPageScript['gift_select_page_script']) ? json_decode($giftSelectPageScript['gift_select_page_script'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_select_page_script_ger'] = !empty($giftSelectPageScript['gift_select_page_script_ger']) ? json_decode($giftSelectPageScript['gift_select_page_script_ger'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_select_page_script_it'] = !empty($giftSelectPageScript['gift_select_page_script_it']) ? json_decode($giftSelectPageScript['gift_select_page_script_it'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_select_page_script_sp'] = !empty($giftSelectPageScript['gift_select_page_script_sp']) ? json_decode($giftSelectPageScript['gift_select_page_script_sp'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_select_page_script_jp'] = !empty($giftSelectPageScript['gift_select_page_script_jp']) ? json_decode($giftSelectPageScript['gift_select_page_script_jp'],true) : json_decode('{"script_1": ""}', true);


            $commentPageScript = !empty($buyerListCol['comment_page_script']) ?
                json_decode($buyerListCol['comment_page_script'], true) :
                json_decode('{"comment_page_script": ""}', true);
            $val['comment_page_script'] = !empty($commentPageScript['comment_page_script']) ? json_decode($commentPageScript['comment_page_script'],true) : json_decode('{"script_1": ""}', true);
            $val['comment_page_script_ger'] = !empty($commentPageScript['comment_page_script_ger']) ? json_decode($commentPageScript['comment_page_script_ger'],true) : json_decode('{"script_1": ""}', true);
            $val['comment_page_script_it'] = !empty($commentPageScript['comment_page_script_it']) ? json_decode($commentPageScript['comment_page_script_it'],true) : json_decode('{"script_1": ""}', true);
            $val['comment_page_script_sp'] = !empty($commentPageScript['comment_page_script_sp']) ? json_decode($commentPageScript['comment_page_script_sp'],true) : json_decode('{"script_1": ""}', true);
            $val['comment_page_script_jp'] = !empty($commentPageScript['comment_page_script_jp']) ? json_decode($commentPageScript['comment_page_script_jp'],true) : json_decode('{"script_1": ""}', true);

            $amazonCommentPageScript = !empty($buyerListCol['amazon_comment_page_script']) ?
                json_decode($buyerListCol['amazon_comment_page_script'], true) :
                json_decode('{"amazon_comment_page_script": ""}', true);
            $val['amazon_comment_page_script'] = !empty($amazonCommentPageScript['amazon_comment_page_script']) ? json_decode($amazonCommentPageScript['amazon_comment_page_script'],true) : json_decode('{"script_1": ""}', true);
            $val['amazon_comment_page_script_ger'] = !empty($amazonCommentPageScript['amazon_comment_page_script_ger']) ? json_decode($amazonCommentPageScript['amazon_comment_page_script_ger'],true) : json_decode('{"script_1": ""}', true);
            $val['amazon_comment_page_script_it'] = !empty($amazonCommentPageScript['amazon_comment_page_script_it']) ? json_decode($amazonCommentPageScript['amazon_comment_page_script_it'],true) : json_decode('{"script_1": ""}', true);
            $val['amazon_comment_page_script_sp'] = !empty($amazonCommentPageScript['amazon_comment_page_script_sp']) ? json_decode($amazonCommentPageScript['amazon_comment_page_script_sp'],true) : json_decode('{"script_1": ""}', true);
            $val['amazon_comment_page_script_jp'] = !empty($amazonCommentPageScript['amazon_comment_page_script_jp']) ? json_decode($amazonCommentPageScript['amazon_comment_page_script_jp'],true) : json_decode('{"script_1": ""}', true);

            $giftProductPageScript = !empty($buyerListCol['gift_product_page_script']) ?
                json_decode($buyerListCol['gift_product_page_script'], true) :
                json_decode('{"gift_product_page_script": ""}', true);
            $val['gift_product_page_script'] = !empty($giftProductPageScript['gift_product_page_script']) ? json_decode($giftProductPageScript['gift_product_page_script'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_product_page_script_ger'] = !empty($giftProductPageScript['gift_product_page_script_ger']) ? json_decode($giftProductPageScript['gift_product_page_script_ger'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_product_page_script_it'] = !empty($giftProductPageScript['gift_product_page_script_it']) ? json_decode($giftProductPageScript['gift_product_page_script_it'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_product_page_script_sp'] = !empty($giftProductPageScript['gift_product_page_script_sp']) ? json_decode($giftProductPageScript['gift_product_page_script_sp'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_product_page_script_jp'] = !empty($giftProductPageScript['gift_product_page_script_jp']) ? json_decode($giftProductPageScript['gift_product_page_script_jp'],true) : json_decode('{"script_1": ""}', true);

            $giftEndPageScript = !empty($buyerListCol['gift_end_page_script']) ?
                json_decode($buyerListCol['gift_end_page_script'], true) :
                json_decode('{"gift_end_page_script": ""}', true);
            $val['gift_end_page_script'] = !empty($giftEndPageScript['gift_end_page_script']) ? json_decode($giftEndPageScript['gift_end_page_script'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_end_page_script_ger'] = !empty($giftEndPageScript['gift_end_page_script_ger']) ? json_decode($giftEndPageScript['gift_end_page_script_ger'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_end_page_script_it'] = !empty($giftEndPageScript['gift_end_page_script_it']) ? json_decode($giftEndPageScript['gift_end_page_script_it'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_end_page_script_sp'] = !empty($giftEndPageScript['gift_end_page_script_sp']) ? json_decode($giftEndPageScript['gift_end_page_script_sp'],true) : json_decode('{"script_1": ""}', true);
            $val['gift_end_page_script_jp'] = !empty($giftEndPageScript['gift_end_page_script_jp']) ? json_decode($giftEndPageScript['gift_end_page_script_jp'],true) : json_decode('{"script_1": ""}', true);

            $list[$key] = $val;
        }

        return array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );

    }


    /**
     * @Notes:获取用户与账号管理配置
     * @Interface getUserAccountConfig
     * @author: wuyu
     * @Time: 2023/6/2   17:54
     */
    public function getOperateConfig()
    {
        $config = [];
        //获取用户信息
        $config['user_list'] = RedisService::getUserList();
        $config['evaluation_process_list'] = $this->evaluationProcess;
        $config['register_process_list'] = $this->registerProcess;
        $config['gift_section_list'] = $this->giftSection;
        $config['star_rating_evaluation_list'] = $this->starRatingEvaluation;
        $config['jump_amazon_comment_list'] = $this->jumpAmazonComment;
        $config['jump_amazon_star_list'] = $this->jumpAmazonStar;
        $config['select_mode_list'] = $this->selectMode;

        //获取品牌
        $OrderAndCustomersOrderService = new OrderAndCustomersOrderService();
        $config['brand_list'] = $OrderAndCustomersOrderService->brandList;

        return message("获取成功", true, $config);
    }

    /**
     * @Notes:添加或编辑
     * @Interface edit
     * @return array|void
     * @author: wuyu
     * @Time: 2023/5/8   16:45
     */
    public function edit()
    {
        try {
            DB::connection($this->model->connection)->beginTransaction(); //开启事务
            $params = request()->all();
            $id = $params['id'] ?? 0;
            $evaluationProcess = $params['evaluation_process'] ?? 0;
            $giftSection = $params['gift_section'] ?? 0;
            $jumpAmazonComment = $params['jump_amazon_comment'] ?? 0;
            $jumpAmazonStar = $params['jump_amazon_star'] ?? 0;
            $createUser = $params['create_user'] ?? 0;
            $starRatingEvaluation = $params['star_rating_evaluation'] ?? 0;
            $selectMode = $params['select_mode'] ?? 0;
            $brand = $params['brand'] ?? '';
            $registerProcess = $params['register_process'] ?? 0;
            $skuList = $params['sku_list'] ?? '';
            if (empty($createUser)) $createUser =$this->model->userId;

            if (empty($skuList)) throw new \Exception('sku不能为空');
            if (empty($createUser)) throw new \Exception('用户不能为空');

            $skuList = str_replace(['；', ',', '，',"\r\n", "\r", "\n"], ';', $skuList);
            $skuArr = array_unique(explode(';',$skuList));
            $exists = $this->model->getOne([
                ['brand', '=', $brand],
                ['create_user', '=', $createUser],
            ]);

            if (!empty($exists) && empty($id)) {
                throw new \Exception('当前用户,页面功能配置已存在,请勿重复添加');
            } elseif (!empty($exists) && $exists['id'] != $id) {
                throw new \Exception('当前用户,页面功能配置已存在,请勿重复添加');
            }

            $data = [
                'evaluation_process'=>$evaluationProcess,
                'brand'=>$brand,
                'gift_section'=>$giftSection,
                'register_process'=>$registerProcess,
                'select_mode'=>$selectMode,
                'star_rating_evaluation'=>$starRatingEvaluation,
                'jump_amazon_comment'=>$jumpAmazonComment,
                'jump_amazon_star'=>$jumpAmazonStar,
            ];

            if (!empty($params['bad_reviews_script'])) $data['bad_reviews_script']['bad_reviews_script'] = json_encode($params['bad_reviews_script']);
            if (!empty($params['bad_reviews_script_ger'])) $data['bad_reviews_script']['bad_reviews_script_ger'] = json_encode($params['bad_reviews_script_ger']);
            if (!empty($params['bad_reviews_script_it'])) $data['bad_reviews_script']['bad_reviews_script_it'] = json_encode($params['bad_reviews_script_it']);
            if (!empty($params['bad_reviews_script_sp'])) $data['bad_reviews_script']['bad_reviews_script_sp'] = json_encode($params['bad_reviews_script_sp']);
            if (!empty($params['bad_reviews_script_jp'])) $data['bad_reviews_script']['bad_reviews_script_jp'] = json_encode($params['bad_reviews_script_jp']);
            $data['bad_reviews_script'] = json_encode($data['bad_reviews_script']);
            $BuyerModel = new BuyerModel();
            $buyerData = [
                'type'=>1,
                'comment_gift_platform_sku'=>json_encode([]),
                'brand_code'=>$brand
            ];
            if (empty($id)) {
                $data['create_user'] =$buyerData['create_user'] = $createUser;
                $data['update_time'] = $buyerData['update_time'] = time();
                $data['create_time'] = $buyerData['create_time'] = time();
                $insertId = $this->model->insertGetId($data);
            } else {
                $data['update_user'] = $buyerData['update_user'] = $createUser;
                $data['update_time'] =$buyerData['update_time'] = time();
                $this->model->where('id', '=', $id)->update($data);
                $insertId = $id;
                $this->model->cacheReset($id, $data, $id);
            }
            if (empty($insertId)) throw new \Exception('操作失败');

            $OperateSkuModel = new OperateSkuModel();
            $OperateSkuModel->where('operate_id','=',$insertId)->delete();
            foreach ($skuArr as $sku){
                $skuData = [
                    'operate_id'=>$insertId,
                    'sku'=>$sku
                ];
                $OperateSkuModel->insert($skuData);
            }

            //新增运营配置
            if (!empty($params['gift_select_page_script'])) $buyerData['gift_select_page_script']['gift_select_page_script'] = json_encode($params['gift_select_page_script']);
            if (!empty($params['gift_select_page_script_ger'])) $buyerData['gift_select_page_script']['gift_select_page_script_ger'] = json_encode($params['gift_select_page_script_ger']);
            if (!empty($params['gift_select_page_script_it'])) $buyerData['gift_select_page_script']['gift_select_page_script_it'] = json_encode($params['gift_select_page_script_it']);
            if (!empty($params['gift_select_page_script_sp'])) $buyerData['gift_select_page_script']['gift_select_page_script_sp'] = json_encode($params['gift_select_page_script_sp']);
            if (!empty($params['gift_select_page_script_jp'])) $buyerData['gift_select_page_script']['gift_select_page_script_jp'] = json_encode($params['gift_select_page_script_jp']);
            $buyerData['gift_select_page_script'] = json_encode($buyerData['gift_select_page_script']);

            if (!empty($params['comment_page_script'])) $buyerData['comment_page_script']['comment_page_script'] = json_encode($params['comment_page_script']);
            if (!empty($params['comment_page_script_ger'])) $buyerData['comment_page_script']['comment_page_script_ger'] = json_encode($params['comment_page_script_ger']);
            if (!empty($params['comment_page_script_it'])) $buyerData['comment_page_script']['comment_page_script_it'] = json_encode($params['comment_page_script_it']);
            if (!empty($params['comment_page_script_sp'])) $buyerData['comment_page_script']['comment_page_script_sp'] = json_encode($params['comment_page_script_sp']);
            if (!empty($params['comment_page_script_jp'])) $buyerData['comment_page_script']['comment_page_script_jp'] = json_encode($params['comment_page_script_jp']);
            $buyerData['comment_page_script'] = json_encode($buyerData['comment_page_script']);

            if (!empty($params['amazon_comment_page_script'])) $buyerData['amazon_comment_page_script']['amazon_comment_page_script'] = json_encode($params['amazon_comment_page_script']);
            if (!empty($params['amazon_comment_page_script_ger'])) $buyerData['amazon_comment_page_script']['amazon_comment_page_script_ger'] = json_encode($params['amazon_comment_page_script_ger']);
            if (!empty($params['amazon_comment_page_script_it'])) $buyerData['amazon_comment_page_script']['amazon_comment_page_script_it'] = json_encode($params['amazon_comment_page_script_it']);
            if (!empty($params['amazon_comment_page_script_sp'])) $buyerData['amazon_comment_page_script']['amazon_comment_page_script_sp'] = json_encode($params['amazon_comment_page_script_sp']);
            if (!empty($params['amazon_comment_page_script_jp'])) $buyerData['amazon_comment_page_script']['amazon_comment_page_script_jp'] = json_encode($params['amazon_comment_page_script_jp']);
            $buyerData['amazon_comment_page_script'] = json_encode($buyerData['amazon_comment_page_script']);

            if (!empty($params['gift_product_page_script'])) $buyerData['gift_product_page_script']['gift_product_page_script'] = json_encode($params['gift_product_page_script']);
            if (!empty($params['gift_product_page_script_ger'])) $buyerData['gift_product_page_script']['gift_product_page_script_ger'] = json_encode($params['gift_product_page_script_ger']);
            if (!empty($params['gift_product_page_script_it'])) $buyerData['gift_product_page_script']['gift_product_page_script_it'] = json_encode($params['gift_product_page_script_it']);
            if (!empty($params['gift_product_page_script_sp'])) $buyerData['gift_product_page_script']['gift_product_page_script_sp'] = json_encode($params['gift_product_page_script_sp']);
            if (!empty($params['gift_product_page_script_jp'])) $buyerData['gift_product_page_script']['gift_product_page_script_jp'] = json_encode($params['gift_product_page_script_jp']);
            $buyerData['gift_product_page_script'] = json_encode($buyerData['gift_product_page_script']);

            if (!empty($params['gift_end_page_script'])) $buyerData['gift_end_page_script']['gift_end_page_script'] = json_encode($params['gift_end_page_script']);
            if (!empty($params['gift_end_page_script_ger'])) $buyerData['gift_end_page_script']['gift_end_page_script_ger'] = json_encode($params['gift_end_page_script_ger']);
            if (!empty($params['gift_end_page_script_it'])) $buyerData['gift_end_page_script']['gift_end_page_script_it'] = json_encode($params['gift_end_page_script_it']);
            if (!empty($params['gift_end_page_script_sp'])) $buyerData['gift_end_page_script']['gift_end_page_script_sp'] = json_encode($params['gift_end_page_script_sp']);
            if (!empty($params['gift_end_page_script_jp'])) $buyerData['gift_end_page_script']['gift_end_page_script_jp'] = json_encode($params['gift_end_page_script_jp']);
            $buyerData['gift_end_page_script'] = json_encode($buyerData['gift_end_page_script']);

            if (!empty($id)){
                $BuyerModel->where('create_user', '=', $createUser)->where('type', '=', 1)->where('brand_code','=',$brand)->update($buyerData);
            }else{
                $BuyerModel->insert($buyerData);
            }

            DB::connection($this->model->connection)->commit(); //事务提交

            return message("保存成功", true, []);

        } catch (\Exception $e) {
            DB::connection($this->model->connection)->rollBack(); //事务回滚
            return message("保存失败:" . $e->getMessage(), false, []);
        }
    }

    public function status()
    {

        $params = request()->all();

        $id = $params['id'] ?? 0;
        $status = $params['status'] ?? 0;
        if (empty($id) || empty($status)) {
            return message("参数不存在", false, []);
        }
        $data = ['status' => $status];
        $this->model->where('id', $id)->update($data);
        $this->model->cacheReset($id, $data, $id);
        return message("操作成功", true, []);
    }


}
